package com.weather.androidweather.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class NetworkUtils {
    private static final String TAG = "NetworkUtils";
    private static final ExecutorService executor = Executors.newCachedThreadPool();

    /**
     * 检查网络是否可用
     */
    public static boolean isNetworkAvailable(Context context) {
        if (context == null) {
            Log.e(TAG, "Context is null");
            return false;
        }
        
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            boolean isConnected = activeNetworkInfo != null && activeNetworkInfo.isConnected();
            Log.d(TAG, "网络状态: " + (isConnected ? "已连接" : "未连接"));
            return isConnected;
        }
        Log.e(TAG, "无法获取网络服务");
        return false;
    }

    /**
     * 检查是否可以连接到指定域名
     */
    public static boolean isHostReachable(String host) {
        try {
            Log.d(TAG, "正在检查主机可达性: " + host);
            InetAddress address = InetAddress.getByName(host);
            boolean isReachable = address.isReachable(3000);
            Log.d(TAG, "主机 " + host + " 可达性: " + isReachable);
            return isReachable;
        } catch (IOException e) {
            Log.e(TAG, "无法连接到主机: " + host, e);
            return false;
        }
    }

    /**
     * 异步检查是否可以连接到指定域名
     */
    public static Future<Boolean> isHostReachableAsync(String host) {
        return executor.submit(() -> {
            try {
                Log.d(TAG, "异步检查主机可达性: " + host);
                InetAddress address = InetAddress.getByName(host);
                boolean isReachable = address.isReachable(3000);
                Log.d(TAG, "主机 " + host + " 异步可达性: " + isReachable);
                return isReachable;
            } catch (IOException e) {
                Log.e(TAG, "异步检查主机连接失败: " + host, e);
                return false;
            }
        });
    }

    /**
     * 检查百度地图API是否可访问
     */
    public static boolean isBaiduMapApiReachable() {
        return isHostReachable("api.map.baidu.com");
    }

    /**
     * 检查百度定位服务是否可访问
     */
    public static boolean isBaiduLocationServiceReachable() {
        return isHostReachable("cnloc.map.baidu.com");
    }

    /**
     * 检查聚合数据API是否可访问
     */
    public static boolean isJuheApiReachable() {
        return isHostReachable("apis.juhe.cn");
    }

    /**
     * 检查HTTP连接是否可用
     */
    public static boolean isHttpConnectionAvailable(String urlString) {
        try {
            Log.d(TAG, "正在检查HTTP连接: " + urlString);
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.setRequestMethod("HEAD");
            int responseCode = connection.getResponseCode();
            boolean isAvailable = (200 <= responseCode && responseCode < 400);
            Log.d(TAG, "HTTP连接状态码: " + responseCode + ", 可用: " + isAvailable);
            return isAvailable;
        } catch (IOException e) {
            Log.e(TAG, "HTTP连接检查失败: " + urlString, e);
            return false;
        }
    }
} 